package cz.muni.fi.pb138.localnews.dto;

import java.util.Date;

import cz.muni.fi.pb138.localnews.Region;

public class ArticleDTO implements Comparable<ArticleDTO> {

	private String name;
	private Date date;
	private String referenceURL;
	private String city;
	private Region region;
	
	/**
	 * Author of the article. This is mandatory.
	 */
	private String author;
	
	public ArticleDTO() {
	}

	public ArticleDTO(String name, Date date, String referenceURL, String city,
			Region region) {
		super();
		this.name = name;
		this.date = date;
		this.referenceURL = referenceURL;
		this.city = city;
		this.region = region;
	}

	/**
	 * Returns string representation of the object.
	 */
	@Override
	public String toString() {
		String toReturn  = "ArticleDTO [name=" + name + ", date=" + date
				+ ", referenceURL=" + referenceURL + ", city=" + city
				+ ", region=" + region;
		if(author!=null)
		{
			toReturn = toReturn + ", author=" + author;
		}
		toReturn = toReturn  + "]";
		return toReturn;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getReferenceURL() {
		return referenceURL;
	}

	public void setReferenceURL(String referenceURL) {
		this.referenceURL = referenceURL;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public Region getRegion() {
		return region;
	}

	public void setRegion(Region region) {
		this.region = region;
	}

	/* (non-Javadoc)
	 * Compares two articles based on their date.
	 * TODO extend to compare secondary based on name
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(ArticleDTO arg0) {
		long timediffer = date.getTime() - arg0.getDate().getTime();
		if (timediffer < Integer.MIN_VALUE || timediffer > Integer.MAX_VALUE) {
			throw new IllegalArgumentException(timediffer
					+ " cannot be cast to int without changing its value.");
		}
		return (int) timediffer;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((region == null) ? 0 : region.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ArticleDTO other = (ArticleDTO) obj;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (region != other.region)
			return false;
		return true;
	}
	
	/**
	 * Sets author for the ArticleDTO
	 * @param author author to be set
	 */
	public void setAuthor(String author) {
		this.author = author;
	}
	
	/**
	 * Returns author of the article
	 * @return author string
	 */
	public String getAuthor() {
		return author;
	}


}
